package com.sm.filemanagelibrary.view;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bm.library.PhotoView;
import com.scorpio.baselibrary.adapter.BasicPagerAdapter;
import com.scorpio.baselibrary.ui.BasicDialog;
import com.scorpio.baselibrary.utils.ViewController;
import com.scorpio.baselibrary.utils.glide.GlideUtil;
import com.sm.filemanagelibrary.R;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * created by zhud on 2019/10/9
 */
public class ImageFilePagerDialog extends BasicDialog {
    private ViewPager viewPager;
    private List<String> images = new ArrayList<>();

    public ImageFilePagerDialog(@NonNull Context context) {
        super(context, R.style.UniversalDialogStyle);
        initCenterMatchLayout();
        viewPager = v.getView(R.id.viewPager);
        viewPager.setAdapter(adapter);
        setCanceledOnTouchOutside(true);
    }

    public void show(List<String> images, int showIndex) {
        this.images = images;
        adapter.notifyDataSetChanged();
        viewPager.setCurrentItem(showIndex);
        super.show();
    }

    @Override
    protected int onCreateLayout() {
        return R.layout.dialog_image_file_pager;
    }

    private PagerAdapter adapter = new BasicPagerAdapter() {

        @Override
        public int getCount() {
            return images == null ? 0 : images.size();
        }

        @Override
        public View onCreateView(ViewGroup container) {
            View view = LayoutInflater.from(container.getContext()).inflate(R.layout.item_file_image_draw, container, false);
            PhotoView photoView = view.findViewById(R.id.photo_view);
            photoView.enable();
            photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            photoView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            container.addView(view);
            return view;
        }

        @Override
        public void onBindView(ViewController holder, int position) {
            holder.setGlideImageLoding(R.id.photo_view, images.get(position));
        }

        @Override
        public boolean isViewHolderChanged(ViewController holder) {
            return true;
        }
    };

}
